package com.lovo.bbs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.NamingException;

import com.lovo.bbs.po.ForumStatPo;
import com.lovo.bbs.util.DbUtil;
import com.lovo.bbs.util.MysqlDb;
import com.lovo.bbs.util.StringUtil;

/**
 * 杂项信息Dao
 * 
 * @author tiancen2001
 * 
 */
public class ForumStatDao {
	public ForumStatDao(){
		
	}
	/**
	 * 返回杂项信息Po
	 * 
	 * @return
	 * @throws SQLException
	 * @throws NamingException
	 */
	public  ForumStatPo getForumInfo() throws NamingException,
			SQLException {
		ForumStatPo po = new ForumStatPo();

		MysqlDb mysqldb = new MysqlDb();
		mysqldb.setPool(true);
		Connection con = DbUtil.getConn(mysqldb);

		String sql = "select 	topicnum, postnum, todaytopicnum,"
				+ " membernum,sitename, sitenotice   from    forumstat  ";
		ResultSet rs = con.prepareStatement(sql).executeQuery();
		if (rs.next()) {
			po.setTopicNum(rs.getInt(1));
			po.setPostNum(rs.getInt(2));
			po.setTodayTopicNum(rs.getInt(3));
			po.setMemberNum(rs.getInt(4));
			po.setSiteName(StringUtil.NoNull(rs.getString(5)));
			po.setSiteNotice(StringUtil.NoNull(rs.getString(6)));
		}
		mysqldb.closeRS(rs);
		mysqldb.closeConnection(con);
		return po;
	}

	/**
	 * 主题数加一,网站今日主题数加一
	 * 
	 * @throws SQLException
	 * @throws NamingException
	 */
	public  int addOneTopic() throws NamingException, SQLException {
		int added = 0;
		MysqlDb mysqldb = new MysqlDb();
		mysqldb.setPool(true);
		Connection con = DbUtil.getConn(mysqldb);

		String sql = "update forumstat set  topicnum=topicnum+1,"
				+ "todaytopicnum=(select count(*) from topic where   "
				+ "year(topicdate)=year(now())  and  month(topicdate)=month(now())  and day(topicdate)=day(now()))+1";
		added = con.prepareStatement(sql).executeUpdate();

		mysqldb.closeConnection(con);
		return added;
	}

	/**
	 * 会员数加一
	 * 
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	public  int addMember() throws NamingException, SQLException {
		int added = 0;
		MysqlDb mysqldb = new MysqlDb();
		mysqldb.setPool(true);
		Connection con = DbUtil.getConn(mysqldb);
		String sql = "update forumstat set  membernum=membernum+1";
		added = con.prepareStatement(sql).executeUpdate();

		mysqldb.closeConnection(con);
		return added;
	}

	/**
	 * 回帖数加一
	 * 
	 * @return
	 * @throws SQLException
	 * @throws NamingException
	 */
	public  int AddOnePost() throws SQLException, NamingException {
		int added = 0;

		MysqlDb mysqldb = new MysqlDb();
		mysqldb.setPool(true);
		Connection con = DbUtil.getConn(mysqldb);
		String sql = "update forumstat set  postnum=postnum+1";
		added = con.prepareStatement(sql).executeUpdate();

		mysqldb.closeConnection(con);

		return added;
	}

	/**
	 * 更新今日主题数
	 * 
	 * @return
	 * @throws SQLException
	 * @throws NamingException
	 */
	public  int calcTodayTopicNum() throws NamingException, SQLException {
		int calced = 0;

		MysqlDb mysqldb = new MysqlDb();
		mysqldb.setPool(true);
		Connection con = DbUtil.getConn(mysqldb);

		String sql = "update forumstat   " + "set todaytopicnum="
				+ "(select count(*) from topic  " + "where  "
				+ "year(topicdate)=year(now())  and   "
				+ "month(topicdate)=month(now())  and  "
				+ "day(topicdate)=day(now())" + ")";

		calced = con.prepareStatement(sql).executeUpdate();

		mysqldb.closeConnection(con);

		return calced;
	}

	/**
	 * 更新主题数
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	public  int calcTopicNum() throws NamingException, SQLException {
		int calced = 0;

		MysqlDb mysqldb = new MysqlDb();
		mysqldb.setPool(true);
		Connection con = DbUtil.getConn(mysqldb);
		String sql = "update forumstat set topicnum=(select count(*) from topic)";
		calced=con.prepareStatement(sql).executeUpdate();
		
		mysqldb.closeConnection(con);
		
		return calced;
	}

	/**
	 * 更新回帖数
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	public  int calcPostNum() throws NamingException, SQLException {
		int calced = 0;

		MysqlDb mysqldb = new MysqlDb();
		mysqldb.setPool(true);
		Connection con = DbUtil.getConn(mysqldb);
		String sql = "update forumstat set postnum=(select count(*) from post)";
	
		calced=con.prepareStatement(sql).executeUpdate();
		
		mysqldb.closeConnection(con);
		
		return calced;
	}

	/**
	 * 更新网站通告
	 * @param notice
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	public  int updateSiteNotice(String notice) throws NamingException, SQLException {
		int updated=0;
		
		MysqlDb mysqldb = new MysqlDb();
		mysqldb.setPool(true);
		Connection con = DbUtil.getConn(mysqldb);
		
		String sql = "update  forumstat set  sitenotice=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1,notice );
		updated = ps.executeUpdate();
		
		mysqldb.closePS(ps);
		mysqldb.closeConnection(con);
		return updated;
	}

	/**
	 * 计算会员数
	 * @return
	 * @throws SQLException 
	 * @throws NamingException 
	 */
	public  int calcMemberNum() throws NamingException, SQLException {
		int calced = 0;
		
		MysqlDb mysqldb = new MysqlDb();
		mysqldb.setPool(true);
		Connection con = DbUtil.getConn(mysqldb);
		
		String sql = "update forumstat set membernum=(select count(*)  from user)";
		calced = con.prepareStatement(sql).executeUpdate();
		
		mysqldb.closeConnection(con);
		return calced;
	}
}
